package de.baumann.browser.model;

import java.io.Serializable;
import java.util.List;

import de.baumann.browser.api.net.NewsApiService;
import de.baumann.browser.api.net.base.ApiBuilder;
import de.baumann.browser.api.net.vo.Like;
import de.baumann.browser.api.net.vo.NewsColumn;
import de.baumann.browser.api.net.vo.NewsItem;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.api.net.CMSApiService;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.Constants;
import de.baumann.browser.utils.InternalStorageUtil;
import de.baumann.browser.utils.SPUtilKt;
import io.reactivex.Observable;

public class NewsModel extends BaseModel {
    private NewsApiService newsApiService;
    private CMSApiService cmsApiService;

    public NewsModel() {
        newsApiService = ApiBuilder.buildNewsService(NewsApiService.class);
        cmsApiService = ApiBuilder.buildService(CMSApiService.class);
    }


    public Observable<Result<List<NewsColumn>>> getColumn() {
        long columnCacheTime = SPUtilKt.getLong("columnCacheTime", 0);
        long currentTime = System.currentTimeMillis();


        if ((currentTime - columnCacheTime) < TIMEDIFFERENCE) {
            List<NewsColumn> newsColumns = (List<NewsColumn>) InternalStorageUtil.getInstance().getSerializable("column");
            return Observable.just(new Result<>("", 1000, newsColumns));
        }

        return newsApiService.getColumn()
                .map(listResult -> {
                    InternalStorageUtil.getInstance().save("column", (Serializable) listResult.getData());
                    SPUtilKt.put("columnCacheTime", System.currentTimeMillis());
                    return listResult;
                }).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<NewsItem>>> getNews(String columnId, int pageNum, int loadType) {
        switch (loadType) {
            case Constants.LOAD_NORMAL:
                List<NewsItem> newsItems = (List<NewsItem>) InternalStorageUtil.getInstance().getSerializable(columnId);
                if (newsItems != null) {
                    getNewsWithCache(columnId, pageNum);
                    return Observable.just(new Result<>("", 1000, newsItems));
                } else {
                    return getNewsWithCache(columnId, pageNum);
                }
            case Constants.LOAD_MORE:
                return newsApiService.getNews(pageNum, 50, columnId).compose(RxSchedulers.ioMain());
            case Constants.LOAD_REFRESH:
                return getNewsWithCache(columnId, pageNum);
            default:
        }
        return newsApiService.getNews(pageNum, 50, columnId).compose(RxSchedulers.ioMain());
    }

    private Observable<Result<List<NewsItem>>> getNewsWithCache(String columnId, int pageNum) {
        return newsApiService.getNews(pageNum, 50, columnId)
                .map(listResult -> {
                    InternalStorageUtil.getInstance().save(columnId, (Serializable) listResult.getData());
                    SPUtilKt.put("newsCacheTime", System.currentTimeMillis());
                    return listResult;
                }).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Like>> queryLiked(String articleId) {
        return this.cmsApiService.queryLiked(articleId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Object>> like(String articleId, int type) {
        return this.cmsApiService.like(articleId, type).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Object>> comment(String userName, String articleId, String content) {
        return this.cmsApiService.comment(userName, articleId, content).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Object>> complaint(String articleId, String content) {
        return this.cmsApiService.complaint(articleId, content).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Object>> addwatchodin(String articleId) {
        return this.cmsApiService.addwatchodin(articleId).compose(RxSchedulers.ioMain());
    }

}
